package com.skeeey.aim.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.skeeey.aim.model.Workload;

public class WorkloadStatistic {
	
	public String checkWorkloadFile(String year, int quarter) {
		String fileName = null;
		File wlFile = new File(AIMUtil.getSystemConfig("data.storage.path") + File.separator + "workload" + File.separator + 
				year + "-" + quarter + ".xlsx"); 
		if(wlFile.exists() && wlFile.isFile()) {
			fileName = year + "-" + quarter + ".xlsx";
		} else {
			wlFile = new File(AIMUtil.getSystemConfig("data.storage.path") + File.separator + "workload" + File.separator + 
					year + "-" + quarter + ".xls");
			if(wlFile.exists() && wlFile.isFile()) {
				fileName = year + "-" + quarter + ".xls";
			}
		}
		return fileName;
	}
	
	public static class PersonalWorkload {
		private String userName;
		private String percentage;
		
		public String getUserName() {
			return userName;
		}
		public void setUserName(String userName) {
			this.userName = userName;
		}
		public String getPercentage() {
			return percentage;
		}
		public void setPercentage(String percentage) {
			this.percentage = percentage;
		}
	}
	
	public Map<String, Integer> getWorkloads(File workloadFile, String suffix) {
		Map<String, Integer> workloads = new HashMap<String, Integer>();
		InputStream in = null;
		try {
			in = new FileInputStream(workloadFile);
			Workbook workbook = null;
			if("xlsx".equals(suffix)) {
				workbook = new XSSFWorkbook(in);
			} else {
				workbook = new HSSFWorkbook(in);
			}
			Sheet sheet = workbook.getSheetAt(0);
			int count = 0;
			for(int i = 3; i < sheet.getLastRowNum(); i++) {
				Row row = sheet.getRow(i);
				String userName = AIMUtil.getValueFrom(row.getCell(9));
				if(workloads.containsKey(userName)) {
					count = count + 1;
					workloads.put(userName, count);
				} else {
					count = 1;
					workloads.put(userName, count);
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(in != null) {
				try {
					in.close();
				} catch (IOException e) {
					// do nothing
				}
			}
		}
		return workloads;
	}
	
	public List<PersonalWorkload> doWorkloadStatistic(File workloadFile, String suffix, Map<String, Integer> workloads) {
		List<PersonalWorkload> result = new ArrayList<PersonalWorkload>();
		Map<String, Integer> planWorkloads = getWorkloads(workloadFile, suffix);
		for(Entry<String, Integer> entry : planWorkloads.entrySet()) {
			PersonalWorkload pw = new PersonalWorkload();
			pw.setUserName(entry.getKey());
			Integer count = workloads.get(entry.getKey());
			if(count != null) {
				pw.setPercentage(getPercent(count.intValue(), entry.getValue().intValue()));
			} else {
				pw.setPercentage("0%");
			}
			result.add(pw);
		}
		return result;
	}
	
	public static class WorkloadInfo {
		private String dealerName;
		private String date;
		
		public String getDealerName() {
			return dealerName;
		}
		public void setDealerName(String dealerName) {
			this.dealerName = dealerName;
		}
		public String getDate() {
			return date;
		}
		public void setDate(String date) {
			this.date = date;
		}
	}
	
	public List<WorkloadInfo> getPlanWorks(File file, String suffix, String userName) {
		List<WorkloadInfo> result = new ArrayList<WorkloadInfo>();
		InputStream in = null;
		try {
			in = new FileInputStream(file);
			Workbook workbook = null;
			if("xlsx".equals(suffix)) {
				workbook = new XSSFWorkbook(in);
			} else {
				workbook = new HSSFWorkbook(in);
			}
			Sheet sheet = workbook.getSheetAt(0);
			for(int i = 3; i < sheet.getLastRowNum(); i++) {
				Row row = sheet.getRow(i);
				String _userName = AIMUtil.getValueFrom(row.getCell(9));
				if(userName.equals(_userName)) {
					WorkloadInfo workloadInfo = new WorkloadInfo();
					workloadInfo.setDealerName(AIMUtil.getValueFrom(row.getCell(8)));
					workloadInfo.setDate(getDateStr(row.getCell(11).getDateCellValue()));
					result.add(workloadInfo);
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(in != null) {
				try {
					in.close();
				} catch (IOException e) {
					// do nothing
				}
			}
		}
		return result;
	}
	
	private String getDateStr(Date date) {
		String r = "-";
		if(date != null) {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			r = format.format(date);
		}
		return r;
	}
	
	public List<WorkloadInfo> getFinishedWork(List<Workload> workloads) {
		List<WorkloadInfo> result = new ArrayList<WorkloadInfo>();
		for(Workload wl : workloads) {
			WorkloadInfo workloadInfo = new WorkloadInfo();
			workloadInfo.setDealerName(wl.getDealerName());
			workloadInfo.setDate(wl.getFinishDate());
			result.add(workloadInfo);
		}
		return result;
	}
	
	public static String getPercent(int num0, int num1) {
		String result  = "0%";
		if(num1 > 0) {
			NumberFormat numberFormat = NumberFormat.getInstance();
			numberFormat.setMaximumFractionDigits(0);
			result = numberFormat.format((double)num0/(double)num1 * 100) + "%";
		}
		return result;
	}
}